/*
 * button.cpp
 *
 *  Created on: 02.06.2012
 *      Author: Roma Jam
 */

#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_dac.h"
#include "led.h"
#include "button.h"
#include "kl_lib.h"
#include "adc.h"

button_t Btn;

void button_t::Init(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(BTN_CLK, ENABLE);
    GPIO_InitStructure.GPIO_Pin = BTN1_PIN;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(BTN_GPIO, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = BTN2_PIN | BTN3_PIN;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
    GPIO_Init(BTN_GPIO, &GPIO_InitStructure);
}

void button_t::Task(void) {
    uint32_t b1Tmr, b2Tmr, b3Tmr;
    if (Delay.Elapsed(&b1Tmr, 200)) {
        if (BTN1_IS_PRESSED() && !KeyWasPressed) {
            Event();
            KeyWasPressed = true;
        }
        else if (!BTN1_IS_PRESSED() && KeyWasPressed) KeyWasPressed = false;
    }
    if (Delay.Elapsed(&b2Tmr, 200)) {
        if (BTN2_IS_PRESSED() && !KeyWasPressed) {
            Event();
            KeyWasPressed = true;
        }
        else if (!BTN2_IS_PRESSED() && KeyWasPressed) KeyWasPressed = false;
    }
    if (Delay.Elapsed(&b2Tmr, 200)) {
        if (BTN3_IS_PRESSED() && !KeyWasPressed) {
            Event();
            KeyWasPressed = true;
        }
        else if (!BTN3_IS_PRESSED() && KeyWasPressed) KeyWasPressed = false;
    }
}

void button_t::Event(void) {
    LEDG_TOGGLE();
}
